package com.caration.shell.workspace2D;

import java.lang.reflect.Array;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import com.caration.shell.Home;
import com.caration.shell.cr3d.SharedData;

public class ScreenLayout extends ViewGroup {
	public static int a = 0;
	public static int b = 0;
	public static int c;
	public static int d;
	private static int g;
	private static int h;
	private static int i;
	private static int j;
	private static int k;
	private static int l;
	private static int m;
	private static int n;
	private static Bitmap sBitmap;
	private static Canvas sCanvas;
	int e[];
	boolean f[][];
	private final Rect mRect = new Rect();
	private final ShellMenuInfo mMenuInfo = new ShellMenuInfo();
	private RectF mRectF;
	private boolean r;
	private com.tsf.shell.workspace3D.f.a s;

	public ScreenLayout(Context context, com.tsf.shell.workspace3D.f.a a1) {
		super(context);
		e = new int[2];
		mRectF = new RectF();
		r = false;
		if (sBitmap == null) {
			sBitmap = Bitmap.createBitmap(512, 512,
					android.graphics.Bitmap.Config.ARGB_8888);
			sCanvas = new Canvas(sBitmap);
		}
		mMenuInfo.f = this;
		s = a1;
		if (a == 0) {
			DisplayMetrics displaymetrics = new DisplayMetrics();
			Home.getInstance().getWindowManager().getDefaultDisplay()
					.getMetrics(displaymetrics);
			int i1 = displaymetrics.widthPixels;
			int k1 = displaymetrics.heightPixels;
			if (i1 <= k1) {
				a = (int) (80F * SharedData.sDensity);
				b = (int) (100F * SharedData.sDensity);
			} else {
				a = (int) (100F * SharedData.sDensity);
				b = (int) (80F * SharedData.sDensity);
			}
			g = (int) (3F * SharedData.sDensity);
			h = (int) (3F * SharedData.sDensity);
			i = 0;
			j = 0;
			k = (int) ((float) (i1 - j) / (float) a);
			l = (int) ((float) (k1 - g - h) / (float) b);
			c = k;
			d = l;
			(new StringBuilder("mAxisCellsX:")).append(k)
					.append("    mAxisCellsY:").append(l).toString();
		}
		setAlwaysDrawnWithCacheEnabled(false);
		int l1 = c;
		int j1 = d;
		int ai[] = new int[2];
		ai[0] = l1;
		ai[1] = j1;
		f = (boolean[][]) Array.newInstance(Boolean.TYPE, ai);
	}

	public static int a(int i1) {
		int k1 = (i1 - i) / (a + m);
		int j1 = k;
		if (k1 < 0)
			k1 = 0;
		if (k1 >= j1)
			k1 = j1 - 1;
		return k1;
	}

	public static void a(int i1, int j1, float af[]) {
		int k1 = i;
		int l1 = g;
		af[0] = (float) (i1 - k1) / (float) (a + m);
		af[1] = (float) ((j1 + b) - l1) / (float) (b + n);
		l1 = k;
		k1 = l;
		if (af[0] < 0F)
			af[0] = 0F;
		if (af[0] >= (float) l1)
			af[0] = l1 - 1;
		if (af[1] < 0F)
			af[1] = 0F;
		if (af[1] >= (float) k1)
			af[1] = k1 - 1;
	}

	public static int[] a(int i1, int j1) {
		int l1 = Math.min(a, b);
		int k1 = (i1 + l1) / l1;
		l1 = (j1 + l1) / l1;
		int ai[] = new int[2];
		ai[0] = k1;
		ai[1] = l1;
		return ai;
	}

	public static int b(int i1) {
		int j1 = (i1 - g) / (b + n);
		int k1 = l;
		if (j1 < 0)
			j1 = 0;
		if (j1 >= k1)
			j1 = k1 - 1;
		return j1;
	}

	public static int d() {
		return a;
	}

	public static int e() {
		return b;
	}

	static int g() {
		return m;
	}

	static int h() {
		return n;
	}

	static int i() {
		return i;
	}

	static int j() {
		return g;
	}

	public final void a() {
		int i1 = getChildCount();
		for (int j1 = 0; j1 < i1; j1++)
			if (getChildAt(j1) instanceof ShellAppAppWidgetHostView) {
				// ShellAppAppWidgetHostView.a();
			}

	}

	public final void a(View view) {
		LayoutParams layoutparams = (LayoutParams) view.getLayoutParams();
		a(layoutparams);
		super.addView(view, layoutparams);
	}

	public final void a(View view, int i1,
			android.view.ViewGroup.LayoutParams layoutparams) {
		LayoutParams layoutparams1 = (LayoutParams) layoutparams;
		layoutparams1.h = true;
		a(layoutparams1);
		super.addView(view, i1, layoutparams);
		invalidate();
	}

	final void a(View view, int ai[]) {
		if (view != null) {
			LayoutParams layoutparams = (LayoutParams) view.getLayoutParams();
			layoutparams.a = ai[0];
			layoutparams.b = ai[1];
			layoutparams.e = false;
			layoutparams.i = true;
			layoutparams.a();
			mRectF.setEmpty();
			view.requestLayout();
		}
	}

	final void a(LayoutParams layoutparams) {
		for (int i1 = layoutparams.b; i1 < layoutparams.b + layoutparams.d; i1++) {
			for (int j1 = layoutparams.a; j1 < layoutparams.a + layoutparams.c; j1++)
				f[j1][i1] = true;
		}
	}

	public final boolean a(int i1, int j1, int k1, int l1) {
		boolean flag;
		label0: {
			flag = false;
			if (i1 + k1 > c || j1 + l1 > d)
				break label0;
			for (int i2 = j1; i2 < j1 + l1; i2++) {
				for (int j2 = i1; j2 < i1 + k1; j2++)
					if (f[j2][i2])
						break label0;
			}
			flag = true;
		}
		return flag;
	}

	public final int[] a(float f1, float f2, int i1, int j1) {
		int ai1[] = new int[2];
		float f3 = (int) f1;
		float f4 = (int) f2;
		float f5 = 3.402823E+038F;
		boolean flag = false;
		int k1 = 0;
		do {
			if (k1 >= 1 + (d - j1))
				break;
			for (int l1 = 0; l1 < 1 + (c - i1);) {
				label0: {
					float f6;
					label1: {
						if (a(l1, k1, i1, j1)) {
							f6 = com.censivn.C3DEngine.h.a.b(f3,
									f4, l1, k1);
							if (f6 < f5)
								break label1;
						}
						flag = flag;
						f5 = f5;
						break label0;
					}
					ai1[0] = l1;
					ai1[1] = k1;
					f5 = f6;
					flag = true;
				}
				l1++;
				f5 = f5;
				flag = flag;
			}

			k1++;
		} while (true);
		int ai[];
		if (!flag)
			ai = null;
		else
			ai = ai1;
		return ai;
	}

	public final int[] a(float f1, float f2, int i1, int j1, int k1, int l1,
			int i2, int j2) {
		boolean flag;
		if (k1 + i2 <= c)
			flag = true;
		else
			flag = false;
		boolean flag1;
		if (l1 + j2 <= d)
			flag1 = flag;
		else
			flag1 = false;
		if (flag1) {
			for (int i3 = l1; i3 < l1 + j2; i3++) {
				for (int k2 = k1; k2 < k1 + i2; k2++)
					f[k2][i3] = false;

			}

		}
		int ai[] = a(f1, f2, i1, j1);
		if (flag1) {
			for (int l2 = l1; l2 < l1 + j2; l2++) {
				for (int j3 = k1; j3 < k1 + i2; j3++)
					f[j3][l2] = true;

			}

		}
		return ai;
	}

	public final int b() {
		return s.m();
	}

	public final void b(View view) {
		Log.w("ddd", (new StringBuilder("dddddd removeWidget:")).append(view)
				.toString());
		b((LayoutParams) view.getLayoutParams());
		super.removeView(view);
		invalidate();
	}

	final void b(LayoutParams layoutparams) {
		for (int j1 = layoutparams.b; j1 < layoutparams.b + layoutparams.d; j1++) {
			for (int i1 = layoutparams.a; i1 < layoutparams.a + layoutparams.c; i1++)
				f[i1][j1] = false;

		}

	}

	public final ShellMenuInfo c() {
		return mMenuInfo;
	}

	final void c(View view) {
		if (view != null)
			((LayoutParams) view.getLayoutParams()).e = false;
		mRectF.setEmpty();
	}

	public final void cancelLongPress() {
		super.cancelLongPress();
		int j1 = getChildCount();
		for (int i1 = 0; i1 < j1; i1++)
			getChildAt(i1).cancelLongPress();

	}

	protected final boolean checkLayoutParams(
			android.view.ViewGroup.LayoutParams layoutparams) {
		return layoutparams instanceof LayoutParams;
	}

	final void d(View view) {
		((LayoutParams) view.getLayoutParams()).e = true;
		mRectF.setEmpty();
	}

	public final void dispatchDraw(Canvas canvas) {
		super.dispatchDraw(canvas);
	}

	protected final boolean drawChild(Canvas canvas, View view, long l1) {
		if (view instanceof m) {
			b b1 = (b) view.getTag();
			if (b1.f())
				ShellRenderHost.o().b(b1);
		}
		return super.drawChild(sCanvas, view, l1);
	}

	public final boolean[][] e(View view) {
		int i1 = c;
		int j1 = d;
		Object obj = new int[2];
		obj[0] = i1;
		obj[1] = j1;
		obj = (boolean[][]) Array.newInstance(Boolean.TYPE, ((int[]) (obj)));
		for (j1 = 0; j1 < d; j1++)
			for (i1 = 0; i1 < c; i1++)
				obj[i1][j1] = f[i1][j1];

		LayoutParams layoutparams = (LayoutParams) view.getLayoutParams();
		j1 = layoutparams.b;
		i1 = layoutparams.a;
		int k1 = layoutparams.c;
		int i2 = layoutparams.d;
		for (int j2 = j1; j2 < j1 + i2; j2++) {
			for (int l1 = i1; l1 < i1 + k1; l1++)
				obj[l1][j2] = false;

		}

		return ((boolean[][]) (obj));
	}

	public final boolean f() {
		return r;
	}

	public final android.view.ViewGroup.LayoutParams generateLayoutParams(
			AttributeSet attributeset) {
		return new LayoutParams(getContext(), attributeset);
	}

	protected final android.view.ViewGroup.LayoutParams generateLayoutParams(
			android.view.ViewGroup.LayoutParams layoutparams) {
		return new LayoutParams(layoutparams);
	}

	// @Override
	// public Object getTag() {
	// return mMenuInfo;
	// }

	public final ViewParent invalidateChildInParent(int ai[], Rect rect) {
		Rect rect1 = new Rect(ai[0] + rect.left, ai[1] + rect.top, ai[0]
				+ rect.right, ai[1] + rect.bottom);
		int i1 = getChildCount();
		for (int j1 = 0; j1 < i1; j1++) {
			View view = getChildAt(j1);
			if (!(view instanceof m))
				continue;
			m m1 = (m) view;
			if (!m1.a
					&& Rect.intersects(new Rect(view.getLeft(), view.getTop(),
							view.getRight(), view.getBottom()), rect1))
				ShellRenderHost.o().a((b) m1.getTag());
		}

		return super.invalidateChildInParent(ai, rect);
	}

	protected final void onAttachedToWindow() {
		super.onAttachedToWindow();
		mMenuInfo.f = this;
	}

	public final boolean onInterceptTouchEvent(MotionEvent motionevent) {
		label0: {
			ShellMenuInfo p1;
			int i1;
			int j1;
			boolean flag;
			label1: {
				label2: {
					if (getVisibility() != 0)
						break label0;
					i1 = motionevent.getAction();
					p1 = mMenuInfo;
					if (i1 != 0) {
						if (i1 == 1) {
							p1.a = null;
							p1.b = 0;
							p1.c = 0;
							p1.d = 0;
							p1.e = 0;
							setTag(p1);
						}
						break label0;
					}
					Object obj = mRect;
					j1 = (int) motionevent.getX() + getScrollX();
					i1 = (int) motionevent.getY() + getScrollY();
					int l1 = -1 + getChildCount();
					View view;
					do {
						if (l1 < 0)
							break label2;
						view = getChildAt(l1);
						if (view.getVisibility() == 0
								|| view.getAnimation() != null) {
							view.getHitRect(((Rect) (obj)));
							if (((Rect) (obj)).contains(j1, i1))
								break;
						}
						l1--;
					} while (true);
					obj = (LayoutParams) view.getLayoutParams();
					p1.a = view;
					p1.b = ((LayoutParams) (obj)).a;
					p1.c = ((LayoutParams) (obj)).b;
					p1.d = ((LayoutParams) (obj)).c;
					p1.e = ((LayoutParams) (obj)).d;
					flag = true;
					break label1;
				}
				flag = false;
			}
			r = flag;
			if (!flag) {
				int ai[] = e;
				int i2 = i;
				int k1 = g;
				ai[0] = (j1 - i2) / (a + m);
				ai[1] = (i1 - k1) / (b + n);
				j1 = k;
				i1 = l;
				if (ai[0] < 0)
					ai[0] = 0;
				if (ai[0] >= j1)
					ai[0] = j1 - 1;
				if (ai[1] < 0)
					ai[1] = 0;
				if (ai[1] >= i1)
					ai[1] = i1 - 1;
				p1.a = null;
				p1.b = ai[0];
				p1.c = ai[1];
				p1.d = 1;
				p1.e = 1;
			}
			setTag(p1);
		}
		return false;
	}

	protected final void onLayout(boolean changed, int l, int t, int r, int b) {
		int count = getChildCount();
		for (int i = 0; i < count; i++) {
			View view = getChildAt(i);
			if (view.getVisibility() == View.GONE)
				continue;
			LayoutParams layoutparams = (LayoutParams) view.getLayoutParams();
			int l2 = layoutparams.f;
			int j2 = layoutparams.g;
			view.layout(l2, j2, l2 + layoutparams.width, j2
					+ layoutparams.height);
			if (layoutparams.i) {
				layoutparams.i = false;
				getLocationOnScreen(e);
			}
		}

	}

	protected final void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		int width = MeasureSpec.getSize(widthMeasureSpec);
		int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		int height = MeasureSpec.getSize(heightMeasureSpec);
		if (widthMode != MeasureSpec.UNSPECIFIED
				&& heightMode != MeasureSpec.UNSPECIFIED) {
			int j2 = -1 + k;
			int l2 = -1 + l;
			n = (height - g - h - b * l) / l2;
			l2 = width - i - j - a * k;
			if (j2 <= 0)
				m = 0;
			else
				m = l2 / j2;
			j2 = getChildCount();
			for (int i = 0; i < j2; i++) {
				View view = getChildAt(i);
				LayoutParams layoutparams = (LayoutParams) view
						.getLayoutParams();
				layoutparams.a();
				if (layoutparams.h) {
					view.setId((0xff & getId()) << 16
							| (0xff & layoutparams.a) << 8 | 0xff
							& layoutparams.b);
					layoutparams.h = false;
				}
				view.measure(MeasureSpec.makeMeasureSpec(layoutparams.width,
						MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
						layoutparams.height, MeasureSpec.EXACTLY));
			}

			setMeasuredDimension(width, height);
			return;
		} else {
			throw new RuntimeException(
					"ScreenLayout cannot have UNSPECIFIED dimensions");
		}
	}

	public final void requestChildFocus(View view, View view1) {
		super.requestChildFocus(view, view1);
		if (view != null) {
			Rect rect = new Rect();
			view.getDrawingRect(rect);
			requestRectangleOnScreen(rect);
		}
	}

	protected final void setChildrenDrawingCacheEnabled(boolean flag) {
		int j1 = getChildCount();
		for (int i1 = 0; i1 < j1; i1++) {
			View view = getChildAt(i1);
			view.setDrawingCacheEnabled(flag);
			view.buildDrawingCache(true);
		}

	}

	protected final void setChildrenDrawnWithCacheEnabled(boolean flag) {
		super.setChildrenDrawnWithCacheEnabled(flag);
	}

	public class LayoutParams extends android.view.ViewGroup.MarginLayoutParams {
		public int a;
		public int b;
		public int c;
		public int d;
		public boolean e;
		public int f;
		public int g;
		boolean h;
		boolean i;

		public final int a(int i1) {
			return (i1 * ScreenLayout.b + (i1 - 1) * ScreenLayout.h())
					- topMargin - bottomMargin;
		}

		public final void a() {
			int k1 = ScreenLayout.a;
			int j2 = ScreenLayout.b;
			int l1 = ScreenLayout.g();
			int j1 = ScreenLayout.h();
			int j3 = ScreenLayout.i();
			int i3 = ScreenLayout.j();
			int l2 = c;
			int i2 = d;
			int i1 = a;
			int k2 = b;
			width = (l2 * k1 + l1 * (l2 - 1)) - leftMargin - rightMargin;
			height = (i2 * j2 + j1 * (i2 - 1)) - topMargin - bottomMargin;
			f = j3 + i1 * (k1 + l1) + leftMargin;
			g = i3 + k2 * (j2 + j1) + topMargin;
		}

		public final int b(int i1) {
			return (i1 * ScreenLayout.a + (i1 - 1) * ScreenLayout.g())
					- leftMargin - rightMargin;
		}

		public LayoutParams(int i1, int j1, int k1, int l1) {
			super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			a = i1;
			b = j1;
			c = k1;
			d = l1;
		}

		public LayoutParams(Context context, AttributeSet attributeset) {
			super(context, attributeset);
			c = 1;
			d = 1;
		}

		public LayoutParams(android.view.ViewGroup.LayoutParams layoutparams) {
			super(layoutparams);
			c = 1;
			d = 1;
		}
	}

}
